package com.kangyi.user.service.Impl;

import com.kangyi.constant.MessageConstant;
import com.kangyi.constant.StatusConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.*;
import com.kangyi.entity.User;
import com.kangyi.exception.AccountException;
import com.kangyi.exception.AccountNotFoundException;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.PasswordErrorException;
import com.kangyi.user.mapper.UserMapper;
import com.kangyi.user.service.UserService;
import com.kangyi.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String phone = userLoginDTO.getPhone();
        String password = userLoginDTO.getPassword();

        User user = userMapper.getUser(null,phone);
        if (user == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return user;
    }

    @Override
    public void register(UserDTO userDTO) {

        User userByName = userMapper.getUser(userDTO.getName(),null);
        if (userByName != null) {
            throw new AccountException(MessageConstant.NICKNAME_EXIST);
        }

        //校验账号是否已存在
        User userByPhone = userMapper.getUser(null,userDTO.getPhone());
        if (userByPhone != null) {
            throw new AccountException(MessageConstant.ACCOUNT_EXIST);
        }


        User user = User.builder()
                .name(userDTO.getName())
                .password(DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes()))
                .phone(userDTO.getPhone())
                .headshot(userDTO.getHeadshot())
                .status(StatusConstant.ENABLE)
                .build();

        userMapper.insert(user);
    }

    @Override
    public void update(UserEditDTO userEditDto) {
        User user = User.builder()
                .id(userEditDto.getId())
                .name(userEditDto.getName())
                .headshot(userEditDto.getHeadshot()).build();
        userMapper.update(user);
    }

    @Override
    public void updateByPassword(PasswordEditDTO passwordEditDTO) {

        if (passwordEditDTO.getOldPassword().equals(passwordEditDTO.getNewPassword())) {
            throw new PasswordErrorException(MessageConstant.NEW_PASSWORD_ERROR);
        }

        passwordEditDTO.setOldPassword(DigestUtils.md5DigestAsHex(passwordEditDTO.getOldPassword().getBytes()));
        passwordEditDTO.setNewPassword(DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes()));

        User user = userMapper.getUsersById(passwordEditDTO.getId());

        if (!user.getPassword().equals(passwordEditDTO.getOldPassword())) {
            throw new PasswordErrorException(MessageConstant.OLD_PASSWORD_ERROR);
        }

        userMapper.updateByPassword(passwordEditDTO);
    }



    @Override
    public void forgotPassword(ForgotPasswordDTO forgotPasswordDTO) {

        User user = userMapper.getUser(null, forgotPasswordDTO.getPhone());
        if (user == null) {
            throw new AccountException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        forgotPasswordDTO.setNewPassword(DigestUtils.md5DigestAsHex(forgotPasswordDTO.getNewPassword().getBytes()));

        userMapper.forgotPassword(forgotPasswordDTO);
    }

    @Override
    public void changePhone(ChangePhoneDTO changePhoneDTO) {

        userMapper.updateByPhone(changePhoneDTO);

    }

    @Override
    public UserVO inquiryUser() {
        Integer currentId = BaseContext.getCurrentId();

        if (currentId == null) {
            throw new DataNotExistException(MessageConstant.TOKEN_IS_NOT_EXIST);
        }

        return userMapper.selectById(currentId);
    }


}
